package com.jumi.microservice.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.dto.reponse.SalesReponse;
import com.jumi.microservice.dto.reponse.api.SelfShopResponse;
import com.jumi.microservice.entity.CommonGroup;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.entity.JmUserSales;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户业绩表 Mapper 接口
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-07-14
 */
public interface JmUserSalesMapper extends BaseMapper<JmUserSales> {

    Page salesPage(Page<JmUserSales> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select ifnull(sum(pay_goods_price),0) sumSales from jm_user_sales ${ew.customSqlSegment}")
    BigDecimal getSumSales(@Param(Constants.WRAPPER) QueryWrapper wrapper);


    Page getSalesByInviterId(Page page,@Param("inviterId") Integer inviterId,@Param(Constants.WRAPPER) QueryWrapper wrapper);

    @Select("select count(1) c from (select order_sn from jm_user_sales ${ew.customSqlSegment} GROUP BY order_sn) a")
    Integer getSumOrderNum(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT  IFNULL(sum(origin_goods_price-pay_goods_price),0) selfMoney FROM `jm_user_sales` ${ew.customSqlSegment}")
    BigDecimal getSelfMoney(@Param(Constants.WRAPPER) QueryWrapper wrapper);

    @Select("select gid,goods_name,goods_image,create_time, (origin_goods_price-pay_goods_price) selfMoney from jm_user_sales ${ew.customSqlSegment}")
    Page<SelfShopResponse> getSelfPage(Page page,@Param(Constants.WRAPPER) QueryWrapper wrapper);

    @Select("select consumer_type type,sum(pay_goods_price) price from jm_user_sales ${ew.customSqlSegment}")
    List<CommonGroup> getSumSalesByConsumerType(@Param(Constants.WRAPPER) QueryWrapper wrapper);
}
